package ExpressFour;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

//文件系统
public class FileSystem {
    private long flowNo = 0;
    private final SimpleDateFormat formatter;
    private INode currentPath ;
    private MyDirectory dir;
    //path is /home/wyr/desktop
    //初始化的时候，默认创建一个文件夹"/"，作为根目录
    FileSystem() {
        formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        dir=new MyDirectory("/", flowNo++, formatter.format(date));
        currentPath = dir;
    }

    boolean addFile(String name) {
        //创建文件
        Date date = new Date((System.currentTimeMillis()));
        MyFile f=new MyFile(name, flowNo++, formatter.format(date),"./");

        if (((MyDirectory) currentPath).add(f)) {
            File file = new File(name);
            try {
                //设置文件的绝对路径
                if (file.exists()) {
                    file.delete();
                }
                file.createNewFile();
                System.out.println("---创建文件成功---");
                f.setPath(file.getPath());//设置文件的绝对路径
                return true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null,
                        "发生IO流错误",
                        "error",
                        JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        return false;
    }

    boolean addDir(String name) {
        Date date = new Date(System.currentTimeMillis());
        MyDirectory dir=new MyDirectory(name, flowNo++, formatter.format(date));
        if (((MyDirectory) currentPath).add(dir)) {
            System.out.println("---创建目录成功---");
            return true;
        } else {

            System.out.println("---创建目录失败/目录已存在---");
            return false;
        }
    }
    void addSomethingToFile(String name,String content){
        boolean flag=false;
        for(int i=0;i<((MyDirectory)currentPath).getList().size();i++){
            if(((MyDirectory)currentPath).getList().get(i).getName().equals(name)){
                if(((MyDirectory)currentPath).getList().get(i).getType().equals(MyFile.type)){
                    flag=true;
                    MyFile myFile=((MyFile)((MyDirectory)currentPath).getList().get(i));
                    myFile.addToFile(content);//添加content的内容到文件中
                    myFile.flush();//刷新保存文件
                    break;
                }
            }
        }
        if(!flag){
            System.out.println("没有该文件");
        }
    }
    String getSomethingFromFile(String name){
        for(int i=0;i<((MyDirectory)currentPath).getList().size();i++){
            if(((MyDirectory)currentPath).getList().get(i).getName().equals(name)){
                //如果是文件
                if(((MyDirectory)currentPath).getList().get(i).getType().equals(MyFile.type)){
                    MyFile myFile=((MyFile)((MyDirectory)currentPath).getList().get(i));
                    return myFile.getFile();
                }
                else
                    return name+" is a directory";
            }
        }
        return "no such a file";
    }

    boolean remove(String name) {
        if(((MyDirectory)currentPath).remove(name)){
            System.out.println("---已删除---");
            return true;
        }
        else{
            System.out.println("---非空目录，无法删除---");
            return false;
        }
    }
    boolean changePath(String name){
        INode save=currentPath;
        boolean flag=false;
        for(int i=0;i<((MyDirectory)this.currentPath).getList().size();i++){
            //当前目录下存在名字
            if(((MyDirectory)this.currentPath).getList().get(i).getName().equals(name)){
                flag=true;
                if(((MyDirectory)this.currentPath).getList().get(i).getType().equals(MyDirectory.type)){
                    this.currentPath=((MyDirectory)this.currentPath).getList().get(i);//跳转路径
                    System.out.println("---更改路径成功---");
                    //是目录，就返回成功
                    return true;
                }
            }
        }
        if(!flag){
            System.out.println("---无法找到该目录/该名称是文件名称---");
        }
        currentPath=save;
        return false;
    }
    String showCurrentPath(){
        StringBuilder str= new StringBuilder();
        for(int i=0;i<((MyDirectory)currentPath).getList().size();i++){
            str.append(((MyDirectory) currentPath).getList().get(i).getName()).append("\t")
                    .append(((MyDirectory) currentPath).getList().get(i).getType()).append("\t")
                    .append(((MyDirectory) currentPath).getList().get(i).getTime()).append("\t")
                    .append(((MyDirectory) currentPath).getList().get(i).getInode())
                    .append("\n");
        }
        return str.toString();
    }
    String getCurrentPath(){
        INode dir= currentPath;
        StringBuilder str= new StringBuilder();
        while(((MyDirectory)dir).getParent()!=null){
            str.insert(0, dir.getName()+"/");
            dir=((MyDirectory)dir).getParent();
        }
        str.insert(0,dir.getName());
        return str.toString();
    }
    void backPath(){
        if(((MyDirectory)this.currentPath).getParent()!=null) {
            this.currentPath = ((MyDirectory) this.currentPath).getParent();
        }
        else{
            System.out.println("---已经在根目录---");
        }
    }

    @Override
    public String toString() {
        return currentPath.toString();
    }

    public static void main(String[] args) throws InterruptedException {
        FileSystem f=new FileSystem();
        System.out.println(f);
        //创建test目录
        f.addDir("test");
        f.addDir("test1");
        f.addDir("test2");
        f.addDir("test3");
        for(int i=0;i<((MyDirectory)f.currentPath).getList().size();i++) {
            System.out.println(((MyDirectory)f.currentPath).getList().get(i).getName());
        }
        System.out.println("-----------------");
        f.addFile("test");
        f.addSomethingToFile("test","hello word");

        System.out.println(f.getSomethingFromFile("test"));
        f.remove("test");
        //Thread.sleep(10000);
        //f.removeFile("test.txt");
        f.changePath("test");

    }
}
